package com.example.companybusiness.operationt.fragment;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.companybusiness.R;
import com.example.companybusiness.operationc.StarbrandFragment;
import com.example.companybusiness.operationl.MediaReportsFragment;
import com.example.companybusiness.operationt.MainActivityHome;
import com.example.companybusiness.operationt.adapter.HotStoriesAdapter;
import com.example.companybusiness.operationt.adapter.StarBrandAdapter;
import com.example.companybusiness.operationt.bean.HotStoriesBean;
import com.example.companybusiness.operationt.bean.StarBrandBean;

import java.util.ArrayList;
import java.util.List;

public class HomeFragment extends Fragment {
    private TextView textViewYM,textViewm,textViewr;
    private LinearLayout linearLayoutp,linearLayouth,linearLayoutq,linearLayouty;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        searchtiaozhuan(view);//搜索跳转
//        starbranditem(view);//品牌列表
        hotstories(view);//报道列表
        return view;
    }
    private void searchtiaozhuan(View view) {
        textViewYM= view.findViewById(R.id.main_layout_search);
        textViewm = view.findViewById(R.id.fragment_home_layout_tv_morep);
        textViewr = view.findViewById(R.id.fragment_home_layout_tv_morer);
        linearLayoutp= view.findViewById(R.id.fragment_home_layout_soupinpai);
        linearLayouth= view.findViewById(R.id.fragment_home_layout_souhangye);
        linearLayoutq= view.findViewById(R.id.fragment_home_layout_souqiye);
        linearLayouty= view.findViewById(R.id.fragment_home_layout_souyingyong);
        textViewYM.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), MainActivityHome.class);
                startActivity(intent);
            }
        });
        textViewm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), StarbrandFragment.class);
                startActivity(intent);
            }
        });
        textViewr.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //获取fragment的实例
                MediaReportsFragment mediaReportsFragment = new MediaReportsFragment();
                //获取Fragment的管理器
                FragmentManager fragmentManager=getFragmentManager();
                //开启fragment的事物,在这个对象里进行fragment的增删替换等操作。
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                //跳转到fragment，第一个参数为所要替换的位置id，第二个参数是替换后的fragment
                fragmentTransaction.replace(R.layout.fragment_home,mediaReportsFragment);
                //提交事物fragmentTransaction
                fragmentTransaction.commit();
            }
        });
        linearLayoutp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), MainActivityHome.class);
                startActivity(intent);
            }
        });
        linearLayouth.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), MainActivityHome.class);
                startActivity(intent);
            }
        });
        linearLayoutq.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), MainActivityHome.class);
                startActivity(intent);
            }
        });
        linearLayouty.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Uri uri = Uri.parse("http://www.qxswt.com");
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                startActivity(intent);
            }
        });
    }
    private void starbranditem(View view){
        RecyclerView recyclerView = view.findViewById(R.id.fragment_home_layout_rec_branding);
        LinearLayoutManager llm = new LinearLayoutManager(getActivity());
        llm.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerView.setLayoutManager(llm);

        List<StarBrandBean> starBrandBeanList = new ArrayList<>();

        for (int i = 0; i < 8; i++)
        {
            StarBrandBean starBrandBean = new StarBrandBean();
            starBrandBean.setBrand(R.mipmap.ic_launcher);
            starBrandBeanList.add(starBrandBean);
        }
        StarBrandAdapter starBrandAdapter = new StarBrandAdapter(getActivity());
        starBrandAdapter.setData(starBrandBeanList);
        recyclerView.setAdapter(starBrandAdapter);
    }

    private void hotstories(View view){
        RecyclerView recyclerView = view.findViewById(R.id.fragment_home_layout_rec_hotstories);

        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

        List<HotStoriesBean> hotStoriesBeanList = new ArrayList<>();

        for (int i = 0; i < 20; i++)
        {
            HotStoriesBean hotStoriesBean = new HotStoriesBean();
            hotStoriesBean.setReported(R.mipmap.ic_launcher);
            hotStoriesBean.setContent("内容内容内容内容内容内容");
            hotStoriesBean.setTime("2019-11-01 15:40");
            hotStoriesBeanList.add(hotStoriesBean);
        }
        HotStoriesAdapter hotStoriesAdapter = new HotStoriesAdapter(getActivity());
        hotStoriesAdapter.setData(hotStoriesBeanList);
        recyclerView.setAdapter(hotStoriesAdapter);
    }
}